/*用户我的会议JS*/
// 会议日历控件id
var confCalendarId;

//相关会议室URL
var boardroomUrl;

// 预定会议URL
var bookConfUrl;

//会议日历URL
var calendarUrl;

// 当前时间
var currentTime;

// 起始时间
var startTime;

// 终止时间
var endTime;

//具体会议室数据
var detailedBoardroomData;

//当前会议室Id
var selectBoradroomIds;

//人数
var sizeScope;

//部门
var departmentId;

//会服时间
var serviceTime;

//已选中会议室
var selectedBoradroom = null;

function setServiceTime(id) {
    serviceTime = id;
}

function setSizeScope(id) {
    sizeScope = id;
}

function setDepartmentId(id) {
    departmentId = id;
}

/**
 * 相关会议室URL
 */
function setBoardroomUrl(url) {
    boardroomUrl = url;
}

/**
 * 设置会议日历URL
 */
function setCalendarUrl(url) {
    calendarUrl = url;
}

/**
 * 设置会议室id
 */
function setCurBoradroomIds(id) {
    selectBoradroomIds = id;
}

/**
 * 设置会议列表URL
 */
function setConfCalendar(id) {
    confCalendarId = id;

    // 点击前一天时间事件
    $(confCalendarId).bind("selectPrevDate", function (event, time1, time2) {
        $("#applicationTable").find("#meetingDay").val(time1.substr(0, 10));
        $("#applicationTable").find("#conferenceDate").val(time1.substr(0, 10));
        startTime = time1;
        endTime = time2;
        refreshUserConference();
    });

    // 点击后一天时间事件
    $(confCalendarId).bind("selectNextDate", function (event, time1, time2) {
        $("#applicationTable").find("#meetingDay").val(time1.substr(0, 10));
        $("#applicationTable").find("#conferenceDate").val(time1.substr(0, 10));
        startTime = time1;
        endTime = time2;
        refreshUserConference();
    });

    $(confCalendarId).bind("boardroomselected", function (event, boardroomInfo) {
        selectedBoradroom = boardroomInfo;
        createApplicationTable(selectedBoradroom);
        refreshUserConference();
    });

    $(confCalendarId).bind("pagechange", function (event, ids) {
        selectBoradroomIds = ids;
        refreshUserConference();
    });

    $(confCalendarId).bind("conferencetimeload", function (event, time) {
        $("#applicationTable").find("#time").val(time);
        if (time == "") {
            if ($("#divBtn").find("#btnOk").find("img").attr("src") == "/foc/resources/images/user/queding.png") {
                $("#divBtn").find("#btnOk").find("img").attr("src", "/foc/resources/images/user/queding_1.png");
            }
        } else {
            if ($("#divBtn").find("#btnOk").find("img").attr("src") == "/foc/resources/images/user/queding_1.png") {
                $("#divBtn").find("#btnOk").find("img").attr("src", "/foc/resources/images/user/queding.png");
            }
        }
    });

}

/**
 *    取得会议室信息
 * @param id
 */
function createBoardroomsInfo() {
    sizeScope = $("#queryTable1").find("#sizeScope").val();
    departmentId = $("#queryTable1").find("#departmentid").val();
    data = getBoardroomData(boardroomUrl);
    if (selectedBoradroom != null) {
        $("#confCalendar").initBoardroom({id: selectedBoradroom.id, data: data, status: 1});
    } else {
        $("#confCalendar").initBoardroom({data: data, status: 1});
    }
}

function initBoardroomsInfo() {
    sizeScope = $("#queryTable1").find("#sizeScope").val();
    departmentId = $("#queryTable1").find("#departmentid").val();
    data = getBoardroomData(boardroomUrl);
    if (selectedBoradroom != null) {
        $("#confCalendar").initBoardroom({id: selectedBoradroom.id, data: data, status: 0});
    } else {
        $("#confCalendar").initBoardroom({data: data, status: 0});
    }
}

/**
 * 绘制申请会议
 */
function createApplicationInfo() {
    var start = $("#applicationTable").find("#startHour").val() + ":" + $("#applicationTable").find("#startMin").val();
    var end = $("#applicationTable").find("#endHour").val() + ":" + $("#applicationTable").find("#endMin").val();

    if (selectedBoradroom != null) {
        $("#confCalendar").drewBookingDayCalendar({
            id: selectedBoradroom.id,
            startTime: start,
            endTime: end,
            interval: serviceTime,
            flag: $("input[name='flag']:checked").val()
        });
    } else {
        $("#confCalendar").drewBookingDayCalendar({
            startTime: start,
            endTime: end,
            interval: serviceTime,
            flag: $("input[name='flag']:checked").val()
        });
    }
}


/**
 * 添加数据
 * @param data
 */
function createApplicationTable(data) {
    if (data == null) {
        $("#applicationTable").find("#boardroomName").val("");
        $("#applicationTable").find("#boardroom").val("");
        $("#boardroomInfoTable").find("#boardroomName").text("");
        $("#boardroomInfoTable").find("#department").text("");
        $("#boardroomInfoTable").find("#area").text("");
        $("#boardroomInfoTable").find("#size").text("");
        $("#boardroomInfoTable").find("#position").text("");
        $("#deviceInfoTable").find("tr").remove();
        if ($("#boardroomImg1").find("#img1").attr("src") != "" && $("#boardroomImg1").find("#img1").attr("src") != null) {
            $("#boardroomImg1").find("#img1").attr("src", "");
        }
        if ($("#boardroomImg2").find("#img2").attr("src") != "" && $("#boardroomImg2").find("#img2").attr("src") != null) {
            $("#boardroomImg2").find("#img2").attr("src", "");
        }
    } else {
        $("#applicationTable").find("#boardroomName").val(data.boardroomName);
        if (data.image2 != null) {
            $("#applicationTable").find("#boardroom").val(data.id + "," + data.boardroomName + "," + data.image2);
        } else {
            $("#applicationTable").find("#boardroom").val(data.id + "," + data.boardroomName);
        }
        $("#boardroomInfoTable").find("#boardroomName").text("会议室名称: " + data.boardroomName);
        if (data.department == "") {
            $("#boardroomInfoTable").find("#department").text("隶属部门: 公用");
        } else {
            $("#boardroomInfoTable").find("#department").text("隶属部门: " + data.department);
        }
        $("#boardroomInfoTable").find("#area").text("会议室面积: " + data.area);
        $("#boardroomInfoTable").find("#size").text("最大人数: " + data.size);
        $("#boardroomInfoTable").find("#position").text("会议室位置: " + data.position);

        $("#deviceInfoTable").find("tr").remove();
        if (data.deviceInfo != "") {
            $("#deviceInfoTable").append("<tr ><td align='left'><td></tr>");
            var values = data.deviceInfo.split(";");
            for (var i = 0; i < values.length; i++) {
                if (values[i] != "") {
                    $("#deviceInfoTable").append("<tr ><td align='left'>" + values[i] + "<td></tr>");
                }
            }
        } else {
            $("#deviceInfoTable").find("#device").text("");
        }

        if ($("#boardroomImg1").find("#img1").attr("src") != "" && $("#boardroomImg1").find("#img1").attr("src") != null) {
            $("#boardroomImg1").find("#img1").attr("src", "");
        }
        if ($("#boardroomImg2").find("#img2").attr("src") != "" && $("#boardroomImg2").find("#img2").attr("src") != null) {
            $("#boardroomImg2").find("#img2").attr("src", "");
        }
        $("#boardroomImg1").find("#img1").attr("data-original", data.image1);
        $("#boardroomImg2").find("#img2").attr("data-original", data.image2);
        $("img.lazy").lazyload();
    }
}


/**
 * 初始化我的会议JS
 */
function initUserApplication(interval) {
    if (interval == null || interval.length == 0) {
        interval = 5000;
    }
    if (currentTime == null) {
        currentTime = new Date();
        startTime = getStartTime(currentTime);
        endTime = getEndTime(currentTime);
    }
    $(confCalendarId).setNavigateDay({
        startTime: startTime,
        endTime: endTime,
        week: currentTime.getDay()
    });
    $("#applicationTable").find("#meetingDay").val(startTime.substr(0, 10));
    $("#applicationTable").find("#conferenceDate").val(startTime.substr(0, 10));
    initBoardroomsInfo();
    data = getConfTableData(calendarUrl);
    $(confCalendarId).drewDayCalendar({data: data, curDateTime: startTime});
    setInterval(function () {
        createBoardroomsInfo();
        refreshUserConference();
    }, interval);
}


/**
 * 取得会议室信息
 */
function getBoardroomData(url) {
    try {
        var obj = $.ajax({
            url: url,
            async: false,
            timeout: 60000,
            data: {sizeScope: sizeScope, departmentId: departmentId}
        }).responseText;
        return $.parseJSON(obj);
    } catch (e) {
        return null;
    }
}

/**
 * 取得会议数据
 */
function getConfTableData(url) {
    try {
        var obj = $.ajax({
            url: url,
            async: false,
            timeout: 60000,
            data: {boardroomId: selectBoradroomIds, startTime: startTime, endTime: endTime}
        }).responseText;
        return $.parseJSON(obj);
    } catch (e) {
        return null;
    }
}


/**
 * 设置会议室默认状态
 */
function refreshUserConference() {

    // 刷新预订列表
    data = getConfTableData(calendarUrl);
    //刷新会议日历
    $(confCalendarId).drewDayCalendar({data: data, curDateTime: startTime});
    createApplicationInfo();

}

/**
 * 获取起始时间
 */
function getStartTime(dt) {
    var startTime = dt;
    return formatDate(startTime) + " 00:00:00";
}

/**
 * 获取终止时间
 */
function getEndTime(dt) {
    var endTime = dt;
    return formatDate(endTime) + " 23:59:59";
}

/**
 * 格式化日期
 */
function formatDate(dt) {
    var curYear = dt.getFullYear();
    var curMonth = (dt.getMonth() + 1);
    if (curMonth < 10) {
        curMonth = "0" + curMonth;
    }
    var curDay = dt.getDate();
    if (curDay < 10) {
        curDay = "0" + curDay;
    }
    return curYear + "-" + curMonth + "-" + curDay;
}